/*
 * Created by JFormDesigner on Wed Jul 03 12:52:48 CST 2024
 */

package scct.hys.ui;

import java.awt.event.*;
import scct.hys.dao.OrderDao;
import scct.hys.dao.ScreeningDao;
import scct.hys.model.Order;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 26984
 */
public class PanelOrder extends JPanel {
    List<Order> orderList=new ArrayList<>();
    DefaultTableModel Searchmodel;
    DefaultTableModel model;
    public PanelOrder(FormMain main) {
        initComponents();
        initView();
    }





    private void initView() {
        model= (DefaultTableModel) tabOrder.getModel();
        model.addColumn("id");
        model.addColumn("影院名称");
        model.addColumn("影片名称");
        model.addColumn("导演");
        model.addColumn("上映时间");
        model.addColumn("开始时间");
        model.addColumn("电影时长");
        model.addColumn("票价");
        model.addColumn("影片类型");
        getTable();
    }
    public void bubbleSort(List<Order> orderList){
        int n=orderList.size();
        for(int i=0;i<n-1;i++){
            for (int j=0;j<n-i-1;j++){
                if (orderList.get(j).getMovieid()>orderList.get(j+1).getMovieid()){
//                    交换相邻元素
                    Order temp=orderList.get(j);
                    orderList.set(j,orderList.get(j+1));
                    orderList.set(j+1,temp);
                }
            }
        }
    }
    private void getTable() {
        model.setRowCount(0);
        OrderDao orderDao=new OrderDao();
        List<Order> allOrders=orderDao.getAllOrder();
        bubbleSort(allOrders);
        for (Order allOrder:allOrders){
            model.addRow(new Object[]{allOrder.getMovieid(),allOrder.getCinemaName(),allOrder.getMovieName(),allOrder.getMovieActor(),allOrder.getMovieReleasedate()
            ,allOrder.getScreeningStarttime(),allOrder.getMovieDuration(),allOrder.getPriceScreening(),allOrder.getMovieType()});
        }
        System.out.println(allOrders);
    }

    private void btnSearch(ActionEvent e) {
        // TODO add your code here
        tabOrder.setModel(model);
        Searchmodel = new DefaultTableModel();
        Searchmodel.addColumn("id");
        Searchmodel.addColumn("影院名称");
        Searchmodel.addColumn("影片名称");
        Searchmodel.addColumn("导演");
        Searchmodel.addColumn("上映时间");
        Searchmodel.addColumn("开始时间");
        Searchmodel.addColumn("电影时长");
        Searchmodel.addColumn("票价");
        Searchmodel.addColumn("影片类型");

        Searchmodel.setRowCount(0);
        String searchkey = txtSrarchkey.getText();
        System.out.println(searchkey);
        String searchlist = (String) cboOrder.getSelectedItem();
        System.out.println(searchlist);
        int row = tabOrder.getRowCount();
        System.out.println("row:" + row);
        int column = tabOrder.getColumnCount();
        System.out.println("column" + column);
        for (int i = 0; i < row; i++) {
            if (tabOrder.getRowCount() < row) {
            } else {

                for (int j = 0; j < column; j++) {
                    if (Objects.equals(searchlist, tabOrder.getColumnName(j))) {
                        System.out.println(0002);
                        System.out.println("单元格的值:" + tabOrder.getValueAt(i, j));
                        String cellvalue = tabOrder.getValueAt(i, j).toString().toLowerCase();
                        System.out.println(cellvalue.contains(searchkey));
                        if (cellvalue.contains(searchkey)) {

                            Object[] rowDate = new Object[tabOrder.getColumnCount()];
                            for (int k = 0; k < tabOrder.getColumnCount(); k++) {
                                rowDate[k] = tabOrder.getValueAt(i, k);
                                System.out.println(rowDate[k]);
                            }
                            Searchmodel.addRow(rowDate);
                            tabOrder.setRowSelectionAllowed(true);
                            break;
                        }
                    }
                }
            }
        }
        tabOrder.setModel(Searchmodel);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        panel1 = new JPanel();
        cboOrder = new JComboBox<>();
        txtSrarchkey = new JTextField();
        btnSearch = new JButton();
        button2 = new JButton();
        scrollPane1 = new JScrollPane();
        tabOrder = new JTable();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing
        . border. EmptyBorder( 0, 0, 0, 0) , "JF\u006frmDes\u0069gner \u0045valua\u0074ion", javax. swing. border. TitledBorder
        . CENTER, javax. swing. border. TitledBorder. BOTTOM, new java .awt .Font ("D\u0069alog" ,java .
        awt .Font .BOLD ,12 ), java. awt. Color. red) , getBorder( )) )
        ;  addPropertyChangeListener (new java. beans. PropertyChangeListener( ){ @Override public void propertyChange (java .beans .PropertyChangeEvent e
        ) {if ("\u0062order" .equals (e .getPropertyName () )) throw new RuntimeException( ); }} )
        ;
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 5));

            //---- cboOrder ----
            cboOrder.setModel(new DefaultComboBoxModel<>(new String[] {
                "id",
                "\u5f71\u9662\u540d\u79f0",
                "\u5f71\u7247\u540d\u79f0",
                "\u5bfc\u6f14",
                "\u5f00\u59cb\u65f6\u95f4",
                "\u7535\u5f71\u65f6\u957f",
                "\u7968\u4ef7",
                "\u5f71\u7247\u7c7b\u578b"
            }));
            panel1.add(cboOrder);

            //---- txtSrarchkey ----
            txtSrarchkey.setColumns(7);
            panel1.add(txtSrarchkey);

            //---- btnSearch ----
            btnSearch.setText("\u67e5\u8be2");
            btnSearch.addActionListener(e -> btnSearch(e));
            panel1.add(btnSearch);

            //---- button2 ----
            button2.setText("text");
            panel1.add(button2);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabOrder);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JPanel panel1;
    private JComboBox<String> cboOrder;
    private JTextField txtSrarchkey;
    private JButton btnSearch;
    private JButton button2;
    private JScrollPane scrollPane1;
    private JTable tabOrder;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
